﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Xts
{
    namespace XInterBank
    {
        namespace TradingClient
        {
            public class XOrderDeliveryAgent_async
            {
                protected Ice.Communicator x_ic;
                protected Ice.Logger x_logger;
                protected IXOrderDeliveryPrx x_orderDeliveryPrx;
                protected IXAmiOrderDeliveryCBHandler x_cbHandler;

                public bool IsValid { get { return x_orderDeliveryPrx != null; } }

                public XOrderDeliveryAgent_async(ref Ice.Communicator ic, ref Ice.Logger logger, 
                    ref IXAmiOrderDeliveryCBHandler cbHandler)
                {
                    x_ic = ic;
                    x_logger = logger;
                    x_orderDeliveryPrx = null;
                    x_cbHandler = cbHandler;
                }

                public bool Initialize(string orderDeliveryDsb)
                {
                    try
                    {
                        Ice.ObjectPrx objPrx = x_ic.stringToProxy(orderDeliveryDsb);
                        x_orderDeliveryPrx = IXOrderDeliveryPrxHelper.checkedCast(objPrx);
                        return true;
                    }
                    catch(Ice.Exception ex)
                    {
                        x_logger.error(ex.Message);
                    }
                    return false;
                }

                public bool SendOrder(XSendingOrder sendingOrder)
                {
                    if(!IsValid)
                    {
                        x_logger.error("The XOrderDeliveryAgent_async object is not valid.");
                        return false;
                    }

                    try
                    {
                        XOrderDelivery_SendOrder_Cookies cookies = new XOrderDelivery_SendOrder_Cookies(sendingOrder.CustomOrderId);
                        XAmiOrderDelivery_SendOrderCallback cb = 
                            new XAmiOrderDelivery_SendOrderCallback(cookies, ref x_cbHandler, ref x_logger);
                        x_orderDeliveryPrx.begin_SendOrder(sendingOrder).whenCompleted(cb.SendOrderCB, cb.FailureSendOrderCB);
                        return true;
                    }
                    catch (Ice.Exception ex)
                    {
                        x_logger.error(ex.Message);
                    }

                    return false;
                }

                public bool SendMarketMakingOrder(XSendingMarketMakingOrder sendingOrder)
                {
                    if (!IsValid)
                    {
                        x_logger.error("The XOrderDeliveryAgent_async object is not valid.");
                        return false;
                    }

                    try
                    {
                        XOrderDelivery_SendOrder_Cookies cookies = new XOrderDelivery_SendOrder_Cookies(sendingOrder.CustomOrderId);
                        XAmiOrderDelivery_SendMarketMakingOrderCallback cb =
                            new XAmiOrderDelivery_SendMarketMakingOrderCallback(cookies, ref x_cbHandler, ref x_logger);
                        x_orderDeliveryPrx.begin_SendMarketMakingOrder(sendingOrder).whenCompleted(
                            cb.SendMarketMakingOrderCB, cb.FailureSendMarketMakingOrderCB);
                        return true;
                    }
                    catch (Ice.Exception ex)
                    {
                        x_logger.error(ex.Message);
                    }

                    return false;
                }

                public bool CancelOrder(XCancelOrderReq req)
                {
                    if (!IsValid)
                    {
                        x_logger.error("The XOrderDeliveryAgent_async object is not valid.");
                        return false;
                    }

                    try
                    {
                        XOrderDelivery_CancelOrder_Cookies cookies = new XOrderDelivery_CancelOrder_Cookies(req);
                        XAmiOrderDelivery_CancelOrderCallback cb = 
                            new XAmiOrderDelivery_CancelOrderCallback(cookies, ref x_cbHandler, ref x_logger);
                        x_orderDeliveryPrx.begin_CancelOrder(req).whenCompleted(cb.CancelOrderCB, cb.FailureCancelOrderCB);
                        return true;
                    }
                    catch (Ice.Exception ex)
                    {
                        x_logger.error(ex.Message);
                    }

                    return false;
                }

                public bool BatchCancelOrder(XCancelOrderReq[] req)
                {
                    if (!IsValid)
                    {
                        x_logger.error("The XOrderDeliveryAgent_async object is not valid.");
                        return false;
                    }

                    try
                    {
                        XOrderDelivery_BatchCancelOrder_Cookies cookies = new XOrderDelivery_BatchCancelOrder_Cookies(req);
                        XAmiOrderDelivery_BatchCancelOrderCallback cb = 
                            new XAmiOrderDelivery_BatchCancelOrderCallback(cookies, ref x_cbHandler, ref x_logger);
                        x_orderDeliveryPrx.begin_BatchCancelOrder(req).whenCompleted(cb.BatchCancelOrderCB, cb.FailureBatchCancelOrderCB);
                        return true;
                    }
                    catch (Ice.Exception ex)
                    {
                        x_logger.error(ex.Message);
                    }

                    return false;
                }
            }
        }
    }
    
}
